package com.newx.test.config;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname Knife4jConfig
 * @Description TODO
 * @Date 2023/11/15 18:37
 * @Created by yxl
 */
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {

    // 定义分隔符,配置Swagger多包
    private static final String splitor = ";";

    public static Predicate<RequestHandler> basePackage(final String basePackage) {
        return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true);
    }

    private static Function<Class<?>, Boolean> handlerPackage(final String basePackage)     {
        return input -> {
            // 循环判断匹配
            for (String strPackage : basePackage.split(splitor)) {
                boolean isMatch = input.getPackage().getName().startsWith(strPackage);
                if (isMatch) {
                    return true;
                }
            }
            return false;
        };
    }

    private static Optional<? extends Class<?>> declaringClass(RequestHandler input) {
        return Optional.fromNullable(input.declaringClass());
    }

    @Bean
    public Docket system(Environment environment) {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //是否启动swagger 默认启动
                .enable(true)
                //所在分组
                .groupName("1.系统管理")
                .select()
                //指定扫描的包路径
                .apis(RequestHandlerSelectors.basePackage("com.newx.auth.license"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(getPars());
    }
    private ApiInfo apiInfo() {
        Contact author = new Contact("小亮", "中国河南", "1069428518@qq.com");
        return new ApiInfo(
                "License授权系统文档",
                "License授权系统文档",
                "1.0",
                "",
                author,
                "",
                "",
                new ArrayList()
        );
    }
    private List<Parameter> getPars(){
        // 添加接口请求头参数配置 没有的话 可以忽略
        ParameterBuilder tokenPar = new ParameterBuilder();
        ParameterBuilder tenantPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("satoken")
                .description("令牌")
                .defaultValue("")
                .modelRef(new ModelRef("string"))
                .parameterType("header").required(false).order(99).build();
        tenantPar.name("tenantId")
                .description("租户id(预留，不做强制要求)")
                .defaultValue("")
                .modelRef(new ModelRef("string"))
                .parameterType("header").required(false).order(99).build();
        pars.add(tokenPar.build());
        pars.add(tenantPar.build());
        return pars;
    }
}
